package com.lyhengtongwl.zqsnews.Jpush;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.ui.activity.MainActivity;
import com.lyhengtongwl.zqsnews.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.Objects;

import cn.jpush.android.api.JPushInterface;

public class JPushReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        switch (Objects.requireNonNull(intent.getAction())) {
            case JPushInterface
                    .ACTION_REGISTRATION_ID:
                break;
            case JPushInterface.ACTION_MESSAGE_RECEIVED://接收的自定义消息
                assert bundle != null;
//                String jsonStr = printMMsg(bundle);
//                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(jsonStr);
//                NewsNotice newsGG = new NewsNotice();
//                newsGG.setNoticeId(jsonObject.getInteger("id"));
//                newsGG.setTitle(jsonObject.getString("title"));
//                newsGG.setContent(jsonObject.getString("content"));
//                newsGG.setTime(jsonObject.getString("time"));
//                newsGG.save();
//                Log.e("Jpush", "onReceive: ");
////                new TitleTextWindow(context).show();
////                new Notice(context).initWindowManager("");
////                NotificationHelper.show(context,newsGG.getTitle(),newsGG.getContent(),newsGG.getNoticeId());
//                NotificationUtils.showOpenHistoryNotice(context.getApplicationContext(), newsGG.getTitle(), newsGG.getContent(), newsGG.getNoticeId());
                break;
            case JPushInterface.ACTION_NOTIFICATION_RECEIVED://接受的通知消息
                assert bundle != null;
                EventBus.getDefault().post("changeState");
                SPUtils.put(App.getContext(), "isHaveMsg", true);
                Intent it_1 = new Intent();
                it_1.setClass(context, MainActivity.class);
                it_1.putExtra("tab", 2);
                it_1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //这一行非常关键，如果不加这个，就不能在这实现Activity的启动。
                context.startActivity(it_1);
                break;
            case JPushInterface.ACTION_NOTIFICATION_OPENED://用户点开了通知消息
                Intent it = new Intent();
                it.setClass(context, MainActivity.class);
                it.putExtra("tab", 2);
                it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //这一行非常关键，如果不加这个，就不能在这实现Activity的启动。
                context.startActivity(it);
                break;
        }
    }



    //获取发送的通知内容信息
    private static String printNotifyMsg(Bundle bundle) {
        for (String key : bundle.keySet()) {
            if (key.equals(JPushInterface.EXTRA_ALERT)) {
                return bundle.getString(key) + "";
            }
        }
        return null;
    }

    //获取发送的自定义消息内容信息
    private static String printMMsg(Bundle bundle) {
        for (String key : bundle.keySet()) {
            if (key.equals(JPushInterface.EXTRA_MESSAGE)) {
                return bundle.getString(key) + "";
            }
        }
        return null;
    }

    // 打印所有的 intent extra 数据
    private static String printBundle(Bundle bundle) {
        StringBuilder sb = new StringBuilder();
        for (String key : bundle.keySet()) {
            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
            } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
            } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
                if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
                    continue;
                }

                try {
                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                    Iterator<String> it = json.keys();

                    while (it.hasNext()) {
                        String myKey = it.next();
                        sb.append("\nkey:" + key + ", value: [" +
                                myKey + " - " + json.optString(myKey) + "]");
                    }
                } catch (JSONException e) {
                }

            } else {
                sb.append("\nkey:" + key + ", value:" + bundle.get(key));
            }
        }
        return sb.toString();
    }
}
